layui.use(['table','form','laydate'], function(){
	var table = layui.table;
	var form = layui.form;
	var laydate = layui.laydate;
	
	//年选择器
	laydate.render({
		elem: '#enrolYear'
		,type: 'year'
	});
	
	/*添加班级加载阶段下拉框*/
	loadstage("stage" , form);
	/*添加班级根据阶段加载学校下拉框*/
	form.on('select(stage)', function(data) {
		loadSchool(data.value , "schoolid" , form);
	});
	
	/*选择学校后选择入学年份的按钮变为可用状态*/
	form.on('select(schoolid)', function(data) {
		var stage = data.value;
		var stage = $("#stage");
		if(stage == 00 || stage == ""){
			layer.alert('请选择阶段！', {icon: 7});
			$("#enrollmentYear").attr("disabled", true); 
			$("#enrollmentYear").addClass("layui-disabled");
		}else{
			$("#enrollmentYear").attr("disabled", false); 
			$("#enrollmentYear").removeClass("layui-disabled");
		}
	});
	
	laydate.render({
		elem: '#enrollmentYear'
		,type: 'year'
	    ,done: function(value, date, endDate){ 
			var stage = $("#stage").find("option:selected").text();
	    	var schoolid = $("#schoolid").val();
	    	var gradeYear = value;
	    	if(stage == null || stage == ""){
				layer.msg("请选择阶段");
			}else if(schoolid == 00 || schoolid == ""){
				layer.msg("请选择学校");
			}else if(gradeYear != ""){
				//动态生成班级id和班级编号
				$.ajax({
					type : 'get',
					url : '../sysclassmanagement/createclassid?gradeYear='+ gradeYear + "&stage=" + stage + "&schoolid=" +schoolid,
					datatype : 'json',
					success : function(classData) {
						if (classData.code == 10001) {
							$("#classid").val(classData.classid);
							$("#classnumber").val(classData.classNumber);
						}
						else{
							layer.alert(classData.msg, {icon: 2});
						}
					}
				});
			}else{
				layer.alert('请选择入学年份！', {icon: 7});
			}
	    }
	});

	$('#addClass').click(function() {
		var stage = $("#stage").find("option:selected").text();
		var schoolid = $("#schoolid").val();
		var gradeYear = $("#enrollmentYear").val() + "-09-01";
		var classid = $("#classid").val();
		var classnumber = $("#classnumber").val();
		var className = $("#className").val();
		if(className != ""){
			$.ajax({
				url: '../sysclassmanagement/addclass',  
		        type: 'get', 
		        dataType: 'json',  
		        data:{schoolid:schoolid,classid:classid,
		        	className:className,classnumber:classnumber,
		        	gradeYear:gradeYear,period:stage},
		        success: function (data) {
		          if (data.code == 10001) {
		        	  layer.alert(data.msg, {icon: 1});
		        	  $("input").val('');
		        	  $("#enrollmentYear").attr("disabled", true); 
		  			  $("#enrollmentYear").addClass("layui-disabled");
		      		  return false;
		          } else {  
		        	  layer.alert(data.msg, {icon: 2});
		        	  $("input").val('');
		        	  $("#enrollmentYear").attr("disabled", true); 
		  			  $("#enrollmentYear").addClass("layui-disabled");
		        	  return false; 
		          }  
		        }
			});  
		}else{
			layer.alert('请输入班级名称！', {icon: 7});
		}
	});
	
	/*查询班级加载阶段下拉框*/
	loadstage("selschooltype" , form);
	/*查询班级根据阶段加载学校下拉框*/
	form.on('select(selschooltype)', function(data) {
		loadSchool(data.value , "selschool" , form);
	});
	
	table.render({
		elem: '#class',
		url: '../sysclassmanagement/getclassbystrwhere',
		title: '班级数据表',
		skin: 'line',
		even: true,
		height:'full-180',
		cols: [
			[{
					type:'numbers',
					title: '序号',
					align: "center",
					width: '10%'
				}, {
					field: 'className',
					title: '班级名称',
					align: "center",
					width: '20%'
				}, {
					field: 'gradeYear',
					title: '入学年份',
					align: "center",
					width: '30%'
				}, {
					field: 'schoolName',
					title: '所属学校',
					align: "center",
					width: '40%'
				}]
		]
	});

	//工具栏事件 查询班级信息
	$("#btnselcla").click(function(){
		var gradeYear = $("#enrolYear").val();
		var selschoolid = $("#selschool").val();
		if(selschoolid == 00 || selschoolid == null){
			selschoolid = "";
		}
		if(gradeYear == null){
			gradeYear = "";
		}
		table.render({
			elem: '#class',
			url : '../sysclassmanagement/getclassbygradeyear?gradeYear='+gradeYear+ "&schoolid="+selschoolid,
			title: '班级数据表',
			skin: 'line',
			even: true,
			height:"full-170",
			cols: [
				[{
					type:'numbers',
					title: '序号',
					align: "center",
					width: '10%'
				}, {
					field: 'className',
					title: '班级名称',
					align: "center",
					width: '20%'
				}, {
					field: 'gradeYear',
					title: '入学年份',
					align: "center",
					width: '30%'
				}, {
					field: 'schoolName',
					title: '所属学校',
					align: "center",
					width: '40%'
				}]
			]
		});
	});
	
});